package com.youbug.system.service.impl;

import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youbug.system.entity.Customer;
import com.youbug.system.mapper.CustomerMapper;
import com.youbug.system.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2023-03-12
 */
@Service
@Transactional(transactionManager="transactionManagerKefu")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private AES aes;

    @Override
    public boolean checkTelephone(String telephone) {
        return check("telephone", telephone);
    }

    @Override
    public boolean checkCustomername(String customerName) {
        return check("customer_name", customerName);
    }

    private boolean check(String mobile, String mobile1) {

        QueryWrapper<Customer> wrapper = new QueryWrapper<>();

        wrapper.select("id").eq(mobile, mobile1);

        Page<Customer> customerPage = new Page<>();

        customerPage.setSize(1L);

        customerPage.setCurrent(1L);

        Page<Customer> page = this.page(customerPage, wrapper);

        return page.getRecords().size() == 0;
    }

    @Override
    public String getVerifyCode(String mobile) {
        // 按照手机号发送短信 mobile
        String verifyCode = String.valueOf((int)(10000 * Math.random()));
        httpSession.setAttribute("verifyCode", verifyCode);
        return verifyCode;
    }

    @Override
    public Customer login(Customer customer) {

        QueryWrapper<Customer> wrapper = new QueryWrapper<>();

        wrapper.eq("customer_name", customer.getCustomerName());

        Customer dbCustomer = this.getOne(wrapper);

        if(dbCustomer == null){
            throw new RuntimeException("用户名或密码错误！");
        }
        String password = aes.decryptStr(dbCustomer.getPassword());

        if(!password.equals(customer.getPassword())){
            throw new RuntimeException("用户名或密码错误！");
        }

        dbCustomer.setPassword(null);
        return dbCustomer;
    }

    @Override
    public Customer logon(Customer customer) {

        String verifyCode = (String)httpSession.getAttribute("verifyCode");

        if(!customer.getVerifyCode().equals(verifyCode)){
            throw new RuntimeException("验证码错误！");
        }
        boolean checkMobile = checkTelephone(customer.getTelephone());
        if(!checkMobile){
            throw new RuntimeException("手机号重复！");
        }
        boolean checkCustomername = checkCustomername(customer.getCustomerName());

        if(!checkCustomername){
            throw new RuntimeException("用户名重复！");
        }
        customer.setPassword(aes.encryptBase64(customer.getPassword().getBytes()));

        boolean save = this.save(customer);
        if(!save){
            throw new RuntimeException("用户注册异常！");
        }
        customer.setPassword(null);
        return customer;
    }

}
